layui.use(['layer', 'form', 'ztree', 'laydate', 'admin', 'ax', 'table', 'func', 'dict', 'formSelects', 'xmSelect','upload'], function () {
    var form = layui.form;
    var $ax = layui.ax;
    var table = layui.table;
    var func = layui.func;
    var upload = layui.upload;
    var $ = jQuery = layui.jquery;
    var dict = layui.dict; //获取自定义模块
    var formSelects = layui.formSelects;
    var xmSelect = layui.xmSelect;
    dict.renderDictAll(); //渲染
    //table的初始化实例

    $('#spRange').val("USEMATERIALS");
    //重新渲染select数据
    form.render('select');

    /**
     * 系统管理--菜单管理
     */
    var Menu = {
        tableId: "menuTable",    //表格id
        condition: {
            menuId: "",
            menuName: "",
            level: ""
        }
    };
    //宽度大小
    var sm = 110;
    var nor = 150;
    var lg = 180;
    var bt = 220;
    /**
     * 初始化表格的列
     */
    Menu.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'message', sort: true, title: '消息内容'},
            {field: 'sendTime', sort: true, title: '发送时间'},
            {field: 'status', sort: true, title: '状态'},
            {field: 'createdAt', sort: true, title: '创建时间', width: 180,},
            {field: 'createdBy', sort: true, title: '店铺'}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Menu.tableId,
        url: Feng.ctxPath + '/erya/message/queryAll',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Menu.initColumn(),
    });


    /**
     * 点击编辑菜单按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    Menu.onEditMenu = function (data) {
        func.open({
            height: 720,
            title: '修改菜单',
            content: Feng.ctxPath + "/banjiang/sp_edit?spNo=" + data.sp_no,
            tableId: Menu.tableId,
            endCallback: function () {
                table.reload(Menu.tableId);
            }
        });
    };

    Menu.onDeleteMenu = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/banjiang/spRemove", function () {
                Feng.success("删除成功!");
                table.reload(Menu.tableId);
            }, function (xhr) {
                Feng.error("删除失败!" + xhr.responseJSON.message + "!");
            });
            ajax.set("spNo", data.sp_no);
            ajax.start();
        };
        Feng.confirm("是否删除该原料" + data.sp_name + "?", operation);
    };



    $('#btnExport').click(function () {
        Menu.exportMenu();
    });

    $('#read').click(function () {
        Menu.readMessage();
    });
    
    Menu.readMessage = function () {
        var dataArray = layui.table.checkStatus('menuTable').data;
        var id = '';
        for (const data of dataArray) {
            id += (data.id + ',')
        }
        if (id == '') {
            Feng.error("请选择一条消息！")
            return;
        }
        $.ajax({
            url: Feng.ctxPath + "/erya/message/readMessage",
            type: "post", // 请求类型
            data: {"id": id},
            dataType: 'json',
            async: true, // 是否异步
            success: function (data) {
                Feng.success("操作成功！");
                table.reload(Menu.tableId);
            },
            error: function (data) {
                Feng.error("操作失败！" + data.responseJSON.message)
            }
        });
    }
    

    // 工具条点击事件
    table.on('tool(menuTable)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Menu.onEditMenu(data);
        } else if (layEvent === 'delete') {
            Menu.onDeleteMenu(data);
        } else if (layEvent === 'spShopAssign') {
            Menu.spShopAssign(data);
        } else if (layEvent === 'copy') {
            Menu.onCopyMenu(data);
        } else if (layEvent === 'limit') {
            Menu.openLimitMenu(data);
        }
    });

});